<?php
namespace Wap\Controller;
use Think\Controller;

class ChongzhiController extends CommonController {
	
	public function chong(){
		
		$ctype = I('ctype',1,'intval');//充值类型 1 抢红包 2转盘  3自由充值 4刮刮乐
		$hbid = I('hbid',0,'intval');	
		$djine = I('djine',0,'intval') * 100;//充值金额
			
		$userid= session('userid');
		$user=M('user_list')->where(array('id' => $userid, ))->find(); //$user=M('user_list')->where("id=$userid")->find();
		if($ctype == 1 || $ctype == 2 || $ctype == 4){
		   $hb = M('hb')->where(array('id' => $hbid, ))->find(); //   $hb = M('hb')->where("id=$hbid")->find();
		   $djine = intval($hb['hzhifue']);
		}
		
		// added by skyrim
		if ($ctype == 1 && $djine > 0){
		    $userzhanghu = M('user_zhanghu')->where(array('userid'=>$userid))->find(); //$userzhanghu = M('user_zhanghu')->where('userid=' . $userid)->find();
		    if(0 <= intval($userzhanghu['uqianchong']) && intval($hb['hzhifue']) <= intval($userzhanghu['uqianchong'])){
		        header('refresh: 0; url=' . U('Index/hb', array('hbid'=>$hbid)));
		        return;
		    }
		}
		// added ends
		if(!empty($sysconfig['zfid']) && !empty($sysconfig['zfkey']) && !empty($sysconfig['zftd']))
      {
		$djine = $djine+rand(1,19);
	  }
		$data['ddanhao'] = $danhao = date('YmdHis').$userid;
		$lddanhao = $danhao;
		$data['userid'] = $userid;
		$data['djine'] = $djine;
		$data['dtime'] = time();
		M('user_chongzhi')->add($data);  
		unset($data);

		//$this->hb = $hb;
		//$this->hbid = $hb['id'];
		//$this->lddanhao = $lddanhao;//---------------------U-----------
		//$this->lddjine = $djine;//---------------------U-----------
		//danhao - jiner - hbid
		$sysconfig = M('sys_config')->find();
		
       if($sysconfig['fastpay']==0){
		//t
		
		$key1="1945c482d3dae61a32aac7c1979762cd";//平台商户的密钥 自填
		//$model= {};
		$model->linkId = $danhao;
		$model->mchno='10095787';//平台商户号 自填
		$model->price= $djine;
		$model->bill_title= ($djine/100).'元商品';
		$model->bill_body= ($djine/100).'元商品';
		$model->pay_type='7';
		$model->nonce_str='asd123';
		$model->notify_url='http://'.$_SERVER['HTTP_HOST'].'/index.php/Wap/Paynotify/notify.html';
		$rawStr=json_decode(json_encode($model));
		$rawStr=$this->object_array($rawStr);
		ksort($rawStr);

		$data="";
		foreach($rawStr as $key=>$value)
		{
			if($value!=null){
				$data.=$key."=".$value."&";
			}
		}
		//echo($data);
		$data.="key=".$key1;
		$model->sign=strtoupper(md5($data));

		$res = $this->post('http://api.5w2019.com/api/wypay/createOrder',$model);
		$res = json_decode($res);
       
		if($res->resultCode == 200){
			header("Location: ".$res->order->pay_link);
          exit();
		}else{
			if (!function_exists('get_openid')) {
			require $_SERVER['DOCUMENT_ROOT'].'/fastpay/Fast_Cofig.php';
			}
			$paydata=array();

			$paydata['uid']= $userid;//支付用户id
			
			$paydata['order_no']= $danhao;//订单号
			
			$paydata['total_fee']= $djine?$djine/100:10;//金额
			
			$paydata['param']="";//其他参数
			
			$paydata['me_back_url']= 'http://'.$_SERVER['HTTP_HOST'];//支付成功后跳转
			
			$paydata['notify_url']= 'http://'.$_SERVER['HTTP_HOST'].'/index.php/Wap/Paynotify/notify2.html';
			
			
			$geturl=fastpay_order($paydata);//获取支付链接
			
			
			
			exit("<meta http-equiv='Refresh' content='0;URL={$geturl}'>");
		
		}
        }else{
       		if (!function_exists('get_openid')) {
			require $_SERVER['DOCUMENT_ROOT'].'/fastpay/Fast_Cofig.php';
			}
			$paydata=array();

			$paydata['uid']= $userid;//支付用户id
			
			$paydata['order_no']= $danhao;//订单号
			
			$paydata['total_fee']= $djine?$djine/100:10;//金额
			
			$paydata['param']="";//其他参数
			
			$paydata['me_back_url']= 'http://'.$_SERVER['HTTP_HOST'];//支付成功后跳转
			
			$paydata['notify_url']= 'http://'.$_SERVER['HTTP_HOST'].'/index.php/Wap/Paynotify/notify2.html';
			
			
			$geturl=fastpay_order($paydata);//获取支付链接
			
			
			
			exit("<meta http-equiv='Refresh' content='0;URL={$geturl}'>");
       
       }
		header("Location: /ldpay/alipay.php?danhao=".$lddanhao."&jiner=".$djine."&hbid=".$hbid);

		$sysconfig = M('sys_config')->find();
		$cwxappid = $sysconfig['cwxappid'];
		$cwxmchid = $sysconfig['cwxmchid'];
		$cwxappkey = $sysconfig['cwxappkey'];
		$cwxappsecret = $sysconfig['cwxappsecret'];
		$uopenid = $user['uopenid'];
		if($sysconfig['cbeipay'] == 2){
		   $cwxappid = $sysconfig['cbeiappid'];
		   $cwxmchid = $sysconfig['cbeimchid'];
		   $cwxappkey = $sysconfig['cbeiappkey'];
		   $cwxappsecret = $sysconfig['cbeiappsecret'];
		   $uopenid = $user['ubeiopenid'];
		}
		
		define('WXAPPID',$cwxappid);//微信appid
		define('WXMCHID',$cwxmchid);//微信商户号
		define('WXKEY',$cwxappkey);//微信支付密钥
		define('WXAPPSECRET',$cwxappsecret);//微信appsectet
		
		vendor("wxjiaoyi.JsApiPay");
		
		$input = new \WxPayUnifiedOrder();
		$input->SetBody("充值");          //商品描述
		$input->SetOut_trade_no($danhao);//商户订单号
		$input->SetTotal_fee($djine);                                 //支付金额-单位分
		$input->SetNotify_url('http://'.$_SERVER['HTTP_HOST'].__ROOT__.'/index.php/Wap/Wxnotify/wxreturn.html');//通知地址 
		$input->SetTrade_type("JSAPI");		
		$input->SetLimit_pay("no_credit");
		$input->SetOpenid($uopenid);
		$order = \WxPayApi::unifiedOrder($input);
		
		if($order[return_code] == 'FAIL'){
		    M('sys_log')->add(array('lbiaoshi'=>'微信支付','lcon'=>var_export($order,true),'ltime'=>time())); 
		}
		
		$jsapipay = new \JsApiPay();
		$jsApiParameters = $jsapipay->GetJsApiParameters($order);
		
		$this->hb = $hb;
		$this->hbid = $hb['id'];
      
        $this->curld = 'http://' . $_SERVER['HTTP_HOST'];
		$this->danhao = $danhao;
		$this->djine = $djine;
      
		$this->sysconfig = $sysconfig;
		$this->jsApiParameters = $jsApiParameters;
		if($ctype == 1){
		   $this->display('Index:pay');
		}
		if($ctype == 2){
		   $this->display('Zhuanpan:pay');
		}
		if($ctype == 3){
		   $this->display('Ucenter:chongzhi');
		}
		if($ctype == 4){
		   $this->display('Guaguale:pay');
		}
	}
	
	function object_array($array) {  
    if(is_object($array)) {  
        $array = (array)$array;  
     } if(is_array($array)) {  
         foreach($array as $key=>$value) {  
             $array[$key] = $this->object_array($value);  
             }  
     }  
     return $array;  
	}
	function getBytes($string) { 
		$bytes = array(); 
		for($i = 0; $i < strlen($string); $i++){ 
			 $bytes[] = ord($string[$i]); 
		} 
		return $bytes; 
	} 
	function post($url, $data){ 
		$postdata =json_encode($data);
		$opts = array('http' =>  
					  array(  
						  'method'  => 'POST',  
						  'header'  => 'content-type: application/x-www-form-urlencoded;charset=UTF-8',  
						  'content' => $postdata  
					  )  
		);  
		$context = stream_context_create($opts);  
		$result = file_get_contents($url, false, $context);  
		return $result;  
	} 
	
	public function chong1(){
		
		$ctype = I('ctype',1,'intval');//充值类型 1 抢红包 2转盘  3自由充值 4刮刮乐
		$hbid = I('hbid',0,'intval');	
		$djine = I('djine',0,'intval') * 100;//充值金额
			
		$userid= session('userid');
		$user=M('user_list')->where(array('id' => $userid, ))->find(); //$user=M('user_list')->where("id=$userid")->find();
		if($ctype == 1 || $ctype == 2 || $ctype == 4){
		   $hb = M('hb')->where(array('id' => $hbid, ))->find(); //   $hb = M('hb')->where("id=$hbid")->find();
		   $djine = intval($hb['hzhifue']);
		}
		
		// added by skyrim
		if ($ctype == 1 && $djine > 0){
		    $userzhanghu = M('user_zhanghu')->where(array('userid'=>$userid))->find(); //$userzhanghu = M('user_zhanghu')->where('userid=' . $userid)->find();
		    if(0 <= intval($userzhanghu['uqianchong']) && intval($hb['hzhifue']) <= intval($userzhanghu['uqianchong'])){
		        header('refresh: 0; url=' . U('Index/hb', array('hbid'=>$hbid)));
		        return;
		    }
		}
		// added ends
		if(!empty($sysconfig['zfid']) && !empty($sysconfig['zfkey']) && !empty($sysconfig['zftd']))
      {
		$djine = $djine+rand(1,19);
	  }
		$data['ddanhao'] = $danhao = date('YmdHis').$userid;
		$lddanhao = $danhao;
		$data['userid'] = $userid;
		$data['djine'] = $djine;
		$data['dtime'] = time();
		M('user_chongzhi')->add($data);  
		unset($data);

		//$this->hb = $hb;
		//$this->hbid = $hb['id'];
		//$this->lddanhao = $lddanhao;//---------------------U-----------
		//$this->lddjine = $djine;//---------------------U-----------
		//danhao - jiner - hbid
		$sysconfig = M('sys_config')->find();
      if(!empty($sysconfig['zfid']) && !empty($sysconfig['zfkey']) && !empty($sysconfig['zftd']))
      {
        //20181109 Tim 新对接掌上云支付
          $this->version='1.0';
    $this->customerid= $sysconfig['zfid'];
    $this->sdorderno=$data['ddanhao'];
    $this->total_fee=number_format($data['djine']/100,2,'.','');
    $this->paytype= $sysconfig['zftd'];
    $this->bankcode= '';
    $this->notifyurl='http://'.$_SERVER['HTTP_HOST'].__ROOT__.'/index.php/Wap/Wxnotify/wxreturn.html';
    $this->returnurl="http://".$_SERVER['HTTP_HOST']."/index.php/Wap/Index/hb/hbid/".$hbid;
    $this->remark='Pay';
    $this->get_code=0;
    $this->sign=md5('version='.$this->version.'&customerid='.$this->customerid.'&total_fee='.$this->total_fee.'&sdorderno='.$this->sdorderno.'&notifyurl='.$this->notifyurl.'&returnurl='.$this->returnurl.'&'.$sysconfig['zfkey']);

          $this->display('Index:newpay');return;
      }
		
		//t
		header("Location: /ldpay/alipay.php?danhao=".$lddanhao."&jiner=".$djine."&hbid=".$hbid);

		$sysconfig = M('sys_config')->find();
		$cwxappid = $sysconfig['cwxappid'];
		$cwxmchid = $sysconfig['cwxmchid'];
		$cwxappkey = $sysconfig['cwxappkey'];
		$cwxappsecret = $sysconfig['cwxappsecret'];
		$uopenid = $user['uopenid'];
		if($sysconfig['cbeipay'] == 2){
		   $cwxappid = $sysconfig['cbeiappid'];
		   $cwxmchid = $sysconfig['cbeimchid'];
		   $cwxappkey = $sysconfig['cbeiappkey'];
		   $cwxappsecret = $sysconfig['cbeiappsecret'];
		   $uopenid = $user['ubeiopenid'];
		}
		
		define('WXAPPID',$cwxappid);//微信appid
		define('WXMCHID',$cwxmchid);//微信商户号
		define('WXKEY',$cwxappkey);//微信支付密钥
		define('WXAPPSECRET',$cwxappsecret);//微信appsectet
		
		vendor("wxjiaoyi.JsApiPay");
		
		$input = new \WxPayUnifiedOrder();
		$input->SetBody("充值");          //商品描述
		$input->SetOut_trade_no($danhao);//商户订单号
		$input->SetTotal_fee($djine);                                 //支付金额-单位分
		$input->SetNotify_url('http://'.$_SERVER['HTTP_HOST'].__ROOT__.'/index.php/Wap/Wxnotify/wxreturn.html');//通知地址 
		$input->SetTrade_type("JSAPI");		
		$input->SetLimit_pay("no_credit");
		$input->SetOpenid($uopenid);
		$order = \WxPayApi::unifiedOrder($input);
		
		if($order[return_code] == 'FAIL'){
		    M('sys_log')->add(array('lbiaoshi'=>'微信支付','lcon'=>var_export($order,true),'ltime'=>time())); 
		}
		
		$jsapipay = new \JsApiPay();
		$jsApiParameters = $jsapipay->GetJsApiParameters($order);
		
		$this->hb = $hb;
		$this->hbid = $hb['id'];
      
        $this->curld = 'http://' . $_SERVER['HTTP_HOST'];
		$this->danhao = $danhao;
		$this->djine = $djine;
      
		$this->sysconfig = $sysconfig;
		$this->jsApiParameters = $jsApiParameters;
		if($ctype == 1){
		   $this->display('Index:pay');
		}
		if($ctype == 2){
		   $this->display('Zhuanpan:pay');
		}
		if($ctype == 3){
		   $this->display('Ucenter:chongzhi');
		}
		if($ctype == 4){
		   $this->display('Guaguale:pay');
		}
	}

}
?>